package com.ms.util.exporter;

import com.ms.domain.viewmodel.FiltersBean;
import com.ms.domain.viewmodel.LabelValueBean;
import com.ms.exception.DpwcBusinessException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.util.List;
import java.util.ResourceBundle;

/**
 * User: MLopez
 * Date: 21/03/13
 */
public class ExcelExporter {
    private static Integer LABEL_COLUMN_INDEX = 0;
    private static Integer VALUE_COLUMN_INDEX = 1;
    static ResourceBundle resourceBundle;

    public static void addFilters(Object document, FiltersBean filtersBean, ResourceBundle bundle) {
        resourceBundle = bundle;
        try {
            HSSFWorkbook workbook = (HSSFWorkbook) document;
            HSSFSheet sheet = workbook.createSheet("Filters");

            int initRowIndex = 0;
            addFiltersTableTo(sheet, filtersBean.getPrincipalFilters(), resourceBundle.getString("label_export_report_principal_filters"), initRowIndex);
            initRowIndex = initRowIndex + filtersBean.getPrincipalFilters().size() + 1;

            addFiltersTableTo(sheet, filtersBean.getAdvancedFilters(), resourceBundle.getString("label_export_report_advanced_filters"), initRowIndex);
        } catch (Exception e) {
            throw new DpwcBusinessException("label_exception_generate_export_report");
        }
    }

    private static void addFiltersTableTo(HSSFSheet sheet, List<LabelValueBean> filterList, String title, int initRowIndex) {
        if (!filterList.isEmpty()) {
            addEmptyRowTo(sheet, initRowIndex++);
            addRowTo(sheet, initRowIndex++, title);
            addTitleRowTo(sheet, initRowIndex++);
            for (int i = 0; i < filterList.size(); i++) {
                HSSFRow row = sheet.createRow(initRowIndex);
                addCellTo(row, LABEL_COLUMN_INDEX, resourceBundle.getString(filterList.get(i).getLabel()));
                addCellTo(row, VALUE_COLUMN_INDEX, filterList.get(i).getValue());
                initRowIndex++;
            }
        }
    }


    private static void addRowTo(HSSFSheet worksheet, int rowIndex, String text) {
        HSSFRow titleRow = worksheet.createRow(rowIndex);
        addCellTo(titleRow, LABEL_COLUMN_INDEX, text);
    }

    private static void addEmptyRowTo(HSSFSheet worksheet, int rowIndex) {
        HSSFRow titleRow = worksheet.createRow(rowIndex);
        addCellTo(titleRow, LABEL_COLUMN_INDEX, " ");
    }

    private static void addTitleRowTo(HSSFSheet worksheet, int rowIndex) {
        HSSFRow titleRow = worksheet.createRow(rowIndex);
        addCellTo(titleRow, LABEL_COLUMN_INDEX, resourceBundle.getString("label_export_report_label_filter_cell"));
        addCellTo(titleRow, VALUE_COLUMN_INDEX, resourceBundle.getString("label_export_report_value_filter_cell"));
    }

    private static HSSFCell addCellTo(HSSFRow row, Integer columnIndex, String value) {
        HSSFCell cell = row.createCell(columnIndex);
        cell.setCellValue(value);
        return cell;
    }

}

